package com.aaa.car.service.impl;

import com.aaa.car.entity.Car;
import com.aaa.car.entity.MaintainRecord;
import com.aaa.car.mapper.CarMapper;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.aaa.car.mapper.MaintainRecordMapper;
import com.aaa.car.service.MaintainRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (MaintainRecord)表服务实现类
 *
 * @author YuXin
 * @since 2020-11-25 19:16:09
 */
@Service("maintainRecordService")
@Transactional
public class MaintainRecordServiceImpl implements MaintainRecordService {
    @Resource
    private MaintainRecordMapper maintainRecordMapper;

    @Resource
    private CarMapper carMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MaintainRecord queryById(Integer id) {
        return this.maintainRecordMapper.queryById(id);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param pageNo         查询当前页
     * @param pageSize       查询每页条数
     * @param maintainRecord 对象实体
     * @return pageInfo 带分页列表
     */
    @Override
    public Result queryAllByParam(Integer pageNo, Integer pageSize, MaintainRecord maintainRecord) {
        if (pageNo == 0 || pageSize == 0) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<MaintainRecord> pageInfo = new PageInfo<>(this.maintainRecordMapper.queryAll(maintainRecord));
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), pageInfo);
    }

    /**
     * 新增数据
     *
     * @param maintainRecord 实例对象
     * @return 实例对象
     */
    @Override
    public MaintainRecord insert(MaintainRecord maintainRecord) {
        Integer carId = maintainRecord.getCarId();
        Car car = new Car();
        car.setId(carId);
        car.setState(2);
        carMapper.update(car);
        this.maintainRecordMapper.insert(maintainRecord);
        return maintainRecord;
    }

    /**
     * 修改数据
     *
     * @param maintainRecord 实例对象
     * @return 实例对象
     */
    @Override
    public MaintainRecord update(MaintainRecord maintainRecord) {
        this.maintainRecordMapper.update(maintainRecord);
        return this.queryById(maintainRecord.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.maintainRecordMapper.deleteById(id) > 0;
    }
}